package com.dykj.fireFighting.web.app;

import com.dykj.fireFighting.api.base.effecttimesetting.service.IEffectiveTimeSettingService;
import com.dykj.fireFighting.api.base.effecttimesetting.vo.EffectiveTimeSettingVo;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.maintenance.dto.RepairRecordDto;
import com.dykj.fireFighting.api.base.maintenance.dto.UrgentMaintenanceDto;
import com.dykj.fireFighting.api.base.maintenance.entity.RepairRecord;
import com.dykj.fireFighting.api.base.maintenance.entity.UrgentMaintenance;
import com.dykj.fireFighting.api.base.maintenance.service.IRepairRecordService;
import com.dykj.fireFighting.api.base.maintenance.service.IUrgentMaintenanceService;
import com.dykj.fireFighting.api.base.maintenance.vo.RepairRecordVo;
import com.dykj.fireFighting.api.base.maintenance.vo.UrgentMaintenanceVo;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 紧急维保记录接口
 *
 * @Author JacksonTu
 * @Date 2021-07-28 08:38:40
 */
@Slf4j
@Api(value = "app紧急维保记录接口", tags = {"app紧急维保记录接口"})
@RestController
@RequestMapping("/app/maintenance/urgentMaintenance")
public class AppUrgentMaintenanceController {

    @DubboReference
    private IUrgentMaintenanceService urgentMaintenanceService;
    @DubboReference
    private IRepairRecordService repairRecordService;
    @DubboReference
    private IEffectiveTimeSettingService iEffectiveTimeSettingService;
    @DubboReference
    private IEnterpriseService enterpriseService;


    /**
     * 紧急维保记录列表
     *
     * @return
     */
    @AutoLog(value = "紧急维保记录-分页列表查询")
    @ApiOperation(value = "紧急维保记录分页列表", notes = "紧急维保记录分页列表")
    @GetMapping("/list")
    public CommonResult<PageVo<UrgentMaintenanceVo>> queryUrgentMaintenanceList(UrgentMaintenanceDto urgentMaintenanceDto) {
        if (ShiroUtil.getUser().getEnterpriseId() != null) {
            urgentMaintenanceDto.setMaintenanceDept(ShiroUtil.getUser().getEnterpriseId().toString());
        }
        PageVo<UrgentMaintenanceVo> page = urgentMaintenanceService.queryUrgentMaintenanceList(urgentMaintenanceDto);
        return CommonResult.success(page);
    }

    /**
     * 保存紧急维保记录信息
     */
    @AutoLog(value = "紧急维保记录-保存紧急维保记录信息")
    @ApiOperation(value = "保存紧急维保记录信息", notes = "保存紧急维保记录信息")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody RepairRecord repairRecord) {
        try {
            UrgentMaintenance urgentMaintenance = urgentMaintenanceService.getById(repairRecord.getUrgentMaintenanceId());
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            Enterprise enterprise = enterpriseService.getInfoByBindingCode(loginUserVo.getBindingCode());
            int isEffective = 1;
            int effectiveTime = 0;
            if (null != enterprise) {
                EffectiveTimeSettingVo effectiveTimeSettingVo = iEffectiveTimeSettingService.isEffective(urgentMaintenance.getCreateDate(), "hiddenDanger", enterprise.getId().toString());
                isEffective = effectiveTimeSettingVo.getIsEffective() == true ? 1 : 0;
                effectiveTime = effectiveTimeSettingVo.getTime();
            }
            urgentMaintenance.setIsEffective(isEffective);
            urgentMaintenance.setHiddenDangerTime(effectiveTime);
            urgentMaintenance.setRectificationDate(new Date());
            String status = repairRecord.getStatus();
            if ("1".equals(status)) {
                urgentMaintenance.setStatus(2);
            } else {
                urgentMaintenance.setStatus(Integer.valueOf(repairRecord.getStatus()));
            }
            urgentMaintenanceService.updateById(urgentMaintenance);

            repairRecord.setId(UUIDUtils.get32UUID());
            repairRecord.setCreateDate(new Date());
            repairRecord.setCreateBy(loginUserVo.getId().toString());
            repairRecord.setHiddenDangerTime(effectiveTime);
            repairRecord.setIsEffective(isEffective);
            repairRecordService.save(repairRecord);

            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }


    /**
     * 维修记录表列表
     *
     * @return
     */
    @AutoLog(value = "维修记录-分页列表查询")
    @ApiOperation(value = "维修记录分页列表", notes = "维修记录分页列表")
    @GetMapping("/queryRepairRecordList")
    public CommonResult<PageVo<RepairRecordVo>> queryRepairRecordList(RepairRecordDto repairRecordDto) {
        repairRecordDto.setMaintenanceDept(ShiroUtil.getUser().getEnterpriseId().toString());
        PageVo<RepairRecordVo> page = repairRecordService.queryRepairRecordList(repairRecordDto);
        return CommonResult.success(page);
    }

    /**
     * 维修记录表列表
     *
     * @return
     */
    @AutoLog(value = "维修记录-到达现场")
    @ApiOperation(value = "维修记录-到达现场", notes = "维修记录-到达现场")
    @PostMapping("/updateArriveTime")
    public CommonResult updateArriveTime(@Valid @RequestBody UrgentMaintenanceDto urgentMaintenanceDto) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date=sdf.format(new Date());
        urgentMaintenanceService.updateArriveTime(urgentMaintenanceDto.getId(),date);
        return CommonResult.success(date);
    }

    /**
     * 获取紧急维保维修记录信息
     *
     * @param id
     * @return
     */
    @AutoLog(value = "获取紧急维保维修记录信息")
    @ApiOperation(value = "获取紧急维保维修记录信息", notes = "获取紧急维保维修记录信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<UrgentMaintenanceVo> getInfoById(@PathVariable("id") String id) {
        UrgentMaintenanceVo urgentMaintenanceVo = urgentMaintenanceService.getInfoById(id);
        return CommonResult.success(urgentMaintenanceVo);
    }

    /**
     * 紧急维保 维保机构当月评价得分详情
     *
     * @param urgentMaintenanceDto
     * @return
     */
    @AutoLog(value = "紧急维保 维保机构当月评价得分详情")
    @ApiOperation(value = "紧急维保 维保机构当月评价得分详情", notes = "紧急维保 维保机构当月评价得分详情")
    @GetMapping("/queryUrgentMaintenanceEvaluationInfoList")
    public CommonResult<List<UrgentMaintenanceVo>> queryUrgentMaintenanceEvaluationInfoList(UrgentMaintenanceDto urgentMaintenanceDto) {
        String unitId = ShiroUtil.getUser().getEnterpriseId().toString();
        urgentMaintenanceDto.setMaintenanceDept(unitId);
        List<UrgentMaintenanceVo> maintenanceEvaluationVoList = urgentMaintenanceService.queryUrgentMaintenanceEvaluationInfoList(urgentMaintenanceDto);
        return CommonResult.success(maintenanceEvaluationVoList);
    }
}
